import { Component, Input, Output, EventEmitter, OnInit, OnChanges, OnDestroy } from '@angular/core';
import { NavController, AlertController, LoadingController,NavParams } from 'ionic-angular';

import { TabsPage } from '../tabs/tabs';
import { EnrollPage } from './enroll';
import { MyApp } from '../../app/app.component';

import { LoginNewService} from "./login-new.service";
import {User} from "./User";
import { TempEditMistakesPage }from '../mistake/temp-edit-mistake/temp-edit-mistake';
import {MistakeComponent} from "../mistake/mistake.component";
import {showMistakeByTimePage} from "../mistake/show-mistakeByTime/show-mistakeByTime";
import {CourseListPage} from "../mistake/course-list/course-list";
import {QuestionSetPage} from "../mistake/question-set/question-set";

/*declare var wx: any;
declare var WeixinJSBridge : any;

wx.ready(function(){
    // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
});

wx.error(function(res){
    // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
});*/

@Component({
  selector: 'page-log-in',
  templateUrl: 'log-in.html',
})
export class LogInPage{
  username : string;
  password : string;

  returnString : string;
  res: string;
  curUser : User;
  constructor(public navCtrl: NavController, public navParams: NavParams, public loginNewService: LoginNewService, public alertCtrl: AlertController, public loadingCtrl: LoadingController) {
    if(this.navParams.get("username")!=null && this.navParams.get("password")!=null){
      this.username = this.navParams.get("username");
      this.password = this.navParams.get("password");
    }
    else{
      this.username = "";
      this.password = "";
    }
    this.res="";
  }
  //   isMatchSuccess(phoneNum : string, password : string):boolean{
  //   if(this.loginService.loginMap.get(phoneNum)!=null && this.loginService.loginMap.get(phoneNum).password==password){
  //     return true;
  //   }
  //   else{
  //     return false;
  //   }
  // }
  getLogin() {
    this.loginNewService.login(this.username,this.password).then(response=>{
      this.res=response;
      console.log("getLogin()");
      //console.log(this.res);
      //this.loginNewService.token = "Bearer "+this.res['content'];
      console.log(this.res);
      let body=this.res.toString();
      console.log("this.body "+this.res);
      if(body=='true'){
        console.log("this.res"+this.res);
        this.loginNewService.isLogined(true);
        console.log(this.loginNewService.token);
        this.loginNewService.token=this.res['content'];
        console.log(this.loginNewService.isLoginedFlag);

        this.loginLoading();//这里执行了一遍loginloading
        console.log(this.loginNewService.isLoginedFlag);
      }
      else{
        this.showAlert();
        this.username=null;
        this.password=null;
      }
    });
    //this.loginLoading();

  }

  showAlert() {
    let alert = this.alertCtrl.create({
      subTitle: '账号/密码不正确！',
      buttons: ['确定']
    });
    alert.present();
  }
  openHomePage(){
    this.getLogin();
  }
  enroll(){
    this.navCtrl.push(EnrollPage);
  }
  // loginLoading() {
  //   // let loader = this.loadingCtrl.create({
  //   //   spinner: 'hide',
  //   //   content: '<font size="4">正在登录...</font>'
  //   // });
  //   // loader.present();
  //   setTimeout(() => {
  //     this.loginNewService.isLogined(true);
  //
  //     console.log(this.loginNewService.token);//执行到这一步
  //     if (this.loginNewService.isLoginedFlag == true) {//token不对
  //       this.loginNewService.getCurUser(this.loginNewService.token).then(curUser => {//到getcurrentuser这一步出错了
  //         this.loginNewService.curUser = curUser;
  //         this.navCtrl.push(QuestionSetPage, {
  //           flag: true,
  //           token: this.loginNewService.token,
  //           curStudent: this.loginNewService.curUser,
  //         });
  //         console.log("Turn to tabspage");
  //         console.log("username" + this.loginNewService.curUser.username)
  //       })
  //     }
  //   }
  //   )}

  loginLoading() {
    // let loader = this.loadingCtrl.create({
    //   spinner: 'hide',
    //   content: '<font size="4">正在登录...</font>'
    // });
    // loader.present();
    setTimeout(() => {
      this.loginNewService.isLogined(true);

      console.log(this.loginNewService.token);
      if(this.loginNewService.isLoginedFlag == true){

        this.navCtrl.push(CourseListPage,{
          flag : true,
          token: this.loginNewService.token,
          username: this.username

        });
        console.log("Turn to tabspage");
        //console.log("username"+this.loginNewService.curUser.username)
      }});
  }


      /*if(this.loginService.isLoginedFlag == true){
       this.loginService.getChildren().then(childList=>{
         this.loginService.curChildList = childList;
         this.navCtrl.push(TabsPage,
         {
           flag : true,
           token: this.loginService.token
         })
       }
       );
      }*/
  //   });
  //   loader.dismiss();
  //
  // }


}
